<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>文件测试</title>
	<meta name="decorator" content="default"/>
	<style type="text/css">
		.nav-tabs li{cursor: pointer;}
		.redClass{color: red;}
		.errorClass{color : #1a99e2;}
	</style>
</head>
<body>
	<sys:message content="${message}"/>
	<div id="content" class="row-fluid">
		
		<div id="right" style="">
			
			
			<div id="queryForm" style="" class="breadcrumb form-search">
				<form action="searchForm">
				<ul class="ul-form">
					<li>
						<label class="control-label">A环境:</label>
						<select id="serverType1" name="serverType" class="input-medium required">
							<option value="http://localhost:18080">local</option>
							<option value="http://localhost:8080">cum-core-local</option>
							<option value="http://a90-internal.curriculum.smc-qa.com.cn">a90</option>
						 	<option value="http://a66-internal.curriculum.smc-qa.com.cn">a66</option>
						 	<option value="http://a11-internal.curriculum.smc-qa.com.cn">a11</option>
						 	<option value="http://a12-internal.curriculum.smc-qa.com.cn">a12</option>
						 	<option value="http://stage-internal.curriculum.smc-qa.com.cn">stage</option>
						 	<option value="http://pre-internal.curriculum.smc.com.cn">pre</option>
						</select>
					</li>
					<li>
						<label class="control-label">B环境:</label>
						<select id="serverType2" name="serverType" class="input-medium required">
						 	<option value="http://a12-internal.cum-core.smc-qa.com.cn">cum-core-a12</option>
						 	<option value="http://stage-internal.cum-core.smc-qa.com.cn">cum-core-stage</option>
						 	<option value="http://pre-internal.cum-core.smc.com.cn">cum-core-pre</option>
						 	<option value="http://a12-internal.curriculum.smc-qa.com.cn">a12</option>
						 	<option value="http://pre-internal.curriculum.smc.com.cn">pre</option>
						 	<option value="http://a66-internal.curriculum.smc-qa.com.cn">a66</option>
						 	<option value="http://curriculum.smc.com.cn">prd</option>
						</select>
					</li>
					
					<li class="clearfix"></li>
				</ul>
				<!-- <div style="text-align: right;display: block;padding-right: 50px;">
						<input id="btnSubmit" class="btn btn-primary" type="button" value="查询" onclick="query(this)"/>
						
						 <input id="btnStartTest" class="btn btn-primary" type="button" value="测试" onclick="startTest()"/>
						
				</div> -->
				</form>
			</div>
			<div class="" style="margin-bottom: 3px;">
				<input id="btnStartTest" class="btn btn-primary" type="button" value="测试" onclick="startTest()"/>&nbsp;
				<input id="btnListPage" class="btn " type="button" value="显示/隐藏列表" onclick="showList()"/>&nbsp;
				<input id="btnFresh" class="btn" type="button" value="刷 新" onclick="fresh()"/>
					<!-- <input id="btnAdd" class="btn btn-primary" type="button" value="新 增" onclick="add()"/>&nbsp;
					<input id="btnEdit" class="btn btn-primary" type="button" value="修 改" onclick="edit()"/>&nbsp;
					<input id="btnDel" class="btn btn-primary" type="button" value="删 除" onclick="del()"/>&nbsp;
					<input id="btnDetail" class="btn " type="button" value="查 看" onclick="detail()"/>&nbsp;
			 -->
			 	<span>总数目</span><span id="allTestNum"></span>
			 	<span>已测数目</span><span id="testNum"></span>
			 	<span>测试进度</span><span id="testPre"></span>
			</div>
			<form id="listForm" method="post">
				<table id="contentTable"
					class="table table-striped table-bordered table-condensed">
					<thead>
						<tr class="tableHead">
							<th style="text-align: center;" onclick="toggleCheckBox('checkboxName',this)">
								<input type="checkbox" checkboxName="sort"
								name="table_form_checkbox_all" value=""></th>
							<th style="text-align: center;">序号</th>
							<th>Method</th>
							<th>分组名</th>
							<th>接口名</th>
							<th>url</th>
							<th>参数</th>
							<th>状态</th>
							<th>操作</th>
						</tr>
					</thead>
					<tbody id="tablechildList">
						
					</tbody>
				</table>
				<!-- <div class="pagination" style="margin-left: 10px;">
					<ul class="ul-form">
					<li><label>size：</label>
						<input name="pageSize" value="10" maxlength="50" class="input-mini" style="width: 50px;">
					</li>
					</ul>
				</div> -->
			</form>
			
			<div style="padding: 50px;">
				error:<div id="errorUrl">
					</div><br>
				timeout:<div id="timeoutUrl">
				</div><br>
				diff:<div id="diffUrl">
				</div>
			</div>
			
			<div> <h3>关注接口</h3> </div>
			<div id="diffId" style="width: 800px;">
			
			
			</div>
			<br>
			<div> <h3>所有接口</h3> </div>
			<div id="allInterfaceId" style="width: 800px;">
			
			
			</div>
			
			<div id="templateShowId" name="templateShowDiv" style="width: 800px;display: ;margin-left: 30px;">
				<table name="contentTable4" style="width: 800px;" class="table table-striped table-bordered table-condensed">
					<tbody name="tablechildList0">
						<tr name="rowNum" class="">
							<td>
								<span name="numNo" class=""></span> &nbsp;&nbsp;
							</td>
							<td colspan="2" name="">
								Url: <span name="url" class="">xxx</span> 
								&nbsp;&nbsp;&nbsp;&nbsp;</td>
							</tr>
					</tbody>
					<tbody name="tablechildList1">
						<tr name="rowNum" class="">
							<td name="p0" style="width: 80px;">
							<input class="btn btn-primary" type="button" status=0 value="启动" 
								onclick="startTestOne(this)"/>
							<input class="btn btn-primary" type="button" status=0 value="详情 " 
								onclick="openDetail(this)"/>	
								</td>
							<td name="p1" style="width: 200px;">A环境</td>
							<td name="p2" style="width: 200px;">B环境</td>
						</tr>
					</tbody>
					<tbody name="tablechildList2">
						<!-- <tr name="rowNum" class="redClass"><td name="p1">p1</td><td name="p2">p2</td></tr> -->
					</tbody>
					<tbody name="tablechildList3">
						<!-- <tr name="rowNum" class="redClass"><td name="p1">p1</td><td name="p2">p2</td></tr> -->
					</tbody>
				</table>
				<table name="contentTable3" class="table table-striped table-bordered table-condensed">
					<tbody name="tablechildList">
						<!-- <tr name="rowNum" class="redClass"><td name="p1">p1</td><td name="p2">p2</td></tr> -->
					</tbody>
				</table>
			</div>
			
			<div style="height: 30px;width: 100%;"></div>
			
			<script type="text/template" id="tablechildTpl">//<!--
					<tr id="tablechildList{{idx}}">
						<td style="text-align:center;" >
							<input type="checkbox" name="sort" attrname="sort" value="">
							<input type="hidden" name="url" attrname="url" value="{{row.url}}"/>
							<input type="hidden" name="className" value="{{row.className}}"/>
							<input type="hidden" name="name" value="{{row.name}}"/>
							<input type="hidden" name="param" value="{{row.param}}"/>
						</td>
						<td style="text-align:center;">
							<span attrname="number">{{row.rowno}}</span>
						</td>
						<td>
							<span attrname="method">{{row.method}}</span>
						</td>
						<td>
							<span attrname="className">{{row.className}}</span>
						</td>
						<td><a target="_blank" href="{{row.name}}"></a>
							<span attrname="name">{{row.name}}</span>
						</td>
						<td>
							<span attrname="path" title="{{row.url}}">{{row.url}}</span>
						</td>
						<td>
							<span attrname="param">{{row.param}}</span>
						</td>
						<td>
							<span attrname="remarks"></span>
						</td>
						<td>
							<span attrname="opt">
								<a href="javascript:void(0);" opt="del" onclick="del(this)"></a>
								<a href="javascript:void(0);" opt="get" onclick="get(this)"></a>
							</span>
						</td>
						//-->
				</script>
		</div>
		
		
		
	</div>
	
<script type="text/javascript">


	//URL
	var listUrl = '${ctx}/test/';
	
	var leftWidth = 180; // 左侧窗口大小
	var htmlObj = $("html"), mainObj = $("#main");
	var frameObj = $("#left, #openClose, #right, #right iframe");
	var dataStr = '${data}';
	var datas = eval('('+dataStr+')'); 
	function wSize(){
		 var strs = getWindowSize().toString().split(",");
		//htmlObj.css({"overflow-x":"hidden", "overflow-y":"hidden"});
		mainObj.css("width","auto");
		frameObj.height(strs[0] - 5);
		var leftWidth = ($("#left").width() < 0 ? 0 : $("#left").width());
		$("#right").width($("#content").width()- leftWidth - $("#openClose").width() -5);
		$(".ztree").width(leftWidth - 10).height(frameObj.height() - 46); 
	}

	var tablechildRowIdx = 0 ;
	var tablechildTpl = $("#tablechildTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
	$(document).ready(function() {
		
		loadTablechildListData(datas);
		//showList();
	});

	
	function loadTablechildListData(data){
		tablechildRowIdx = 0;
		var len = jQuery('#tablechildList').children().length;
		for (var i=0; i<data.length; i++){
			addRow('#tablechildList', tablechildRowIdx, tablechildTpl, data[i]);
			tablechildRowIdx = tablechildRowIdx + 1;
		}
	}
	
	function addRow(list, idx, tpl, row){ //新增行
		if(row == null){ row = {};}
		if(row.index == null){ row.index = idx; }
		if(row.rowno == null){ row.rowno = idx+1; }
		row.deleteOptName = row.delFlag == "1"?"撤销":"删除";
		$(list).append(Mustache.render(tpl, {
			idx: idx, delBtn: true, row: row
		}));
		$(list+idx).find("select").each(function(){
			$(this).val($(this).attr("data-value"));
			$(this).select2();
		});
		$(list+idx).find("input[type='checkbox'], input[type='radio']").each(function(){
			if($(this).attr("data-value") == null){
				return ;
			}
			var ss = $(this).attr("data-value").split(',');
			for (var i=0; i<ss.length; i++){
				if($(this).val() == ss[i]){
					$(this).attr("checked","checked");
				}
			}
		});
	}
	
	function delRow(curentTr){ //删除行
		var id = jQuery('[attrname="id"]', curentTr).val();
		var delFlag = jQuery(curentTr).find('[name*=".delFlag"]').val();
		if(id == null || id ==''){
			jQuery(curentTr).remove(); 
		}else if(delFlag == '1'){
			jQuery(curentTr).find('[name*=".delFlag"]').val(0);
			jQuery(curentTr).removeClass("error");
			jQuery(curentTr).find('[name="deleteLine"]').html('删除');
		}else {
			jQuery(curentTr).find('[name*=".delFlag"]').val(1);
			jQuery(curentTr).addClass("error");
			jQuery(curentTr).find('[name="deleteLine"]').html('撤销删除');
		}
		updateTrRowno(jQuery(curentTr).parents('tbody')[0]);
	}
	
	function updateTrRowno(tbody){ //更新行号
		jQuery(tbody).find('[attrname="number"]').each(function(i,v){
			jQuery(this).html(i+1);
		});
	}
	
	function fresh(){
		var url = listUrl +"index?type="+'${type}'+'&status='+'${status}';
		window.location.href = url;
	}
	
	function startTestOne(obj){
		var templateShowDiv = jQuery(obj).parents('[name="templateShowDiv"]')[0];
		var templateShowObj = jQuery(templateShowDiv);
		
		//var url = jQuery('[name="url"]',templateShowDiv).html();
		var url = templateShowObj.attr('url');
		var aUrl = jQuery('#serverType1').val();
		var bUrl = jQuery('#serverType2').val();
		
		url = decodeURI(url);
		
		var r1 = getData(aUrl,url);
		var r2 = getData(bUrl,url);
		var result = Compare(r1, r2);
		
		console.info('url = ',url,' , data = ',result);
		loadShowDiffHtmlObj(templateShowObj,result,url);
		//templateShowObj.attr('id','url'+i);
		//jQuery('[name="numNo"]', templateShowObj).html(i+1);
		//jQuery('#'+allInterfaceId).append(templateShowObj);
		
	}
	
	function startTest(){
		var table = jQuery("#contentTable");
		var checkboxs = jQuery('input[attrname="sort"]:checked', table); //
		//checkboxs = jQuery('input[attrname="sort"]', table); //
		if (checkboxs.length == 0) {
			alertx("请选择至少一行数据！");
			return;
		}
		
		var aUrl = jQuery('#serverType1').val();
		var bUrl = jQuery('#serverType2').val();
		
		var diffId = 'diffId';
		var allInterfaceId = 'allInterfaceId';
		
		jQuery('#'+allInterfaceId).html("");
		jQuery('#'+diffId).html("");
		
		var urls = [];
		var timeoutUrls = [];
		var errorUrls = [];
		var diffUrls = [];
		jQuery('#allTestNum').html(checkboxs.length);
		
		for (var i = 0; i < checkboxs.length; i++) {
			var curentTr = jQuery(checkboxs[i]).parents('tr')[0];
			var url = jQuery('[attrname="url"]', curentTr).val();
			urls.push(url);
			
			jQuery('#testNum').html(urls.length);
			jQuery('#testPre').html(Math.round((urls.length*100/checkboxs.length)) +'%');
			if('/api/teacher/course/getSNCourseIdMap' != url ){
				//continue;
			}
			
			var r1 = getData(aUrl,url);
			var r2 = getData(bUrl,url);
			var result = Compare(r1, r2);
			
			console.info('url = ',url,' , data = ',result);
			var templateShowObj = getShowDiffHtmlObj(result,url);
			templateShowObj.attr('id','url'+i);
			templateShowObj.attr('url',url);
			jQuery('[name="numNo"]', templateShowObj).html(i+1);
			jQuery('#'+allInterfaceId).append(templateShowObj);
			
			
			var testUrlFlag = false;;
			var r1Class= (result.r1.status!=200 || result.r1.useTime>2000) ? "errorClass":"";
			var r2Class= (result.r2.status!=200 || result.r2.useTime>2000) ? "errorClass":"";
			if(result.r1.status!=200 || result.r2.status!=200){
				var statusp = '';
				statusp += result.r1.status+'-';
				statusp += result.r2.status+'-';
				errorUrls.push(statusp+url);
				testUrlFlag = true;
				
			}
			if(result.r1.useTime>2000 || result.r2.useTime>2000){
				var timeoutp = '';
				timeoutp +=result.r1.useTime+'-';
				timeoutp +=result.r2.useTime+'-';
				timeoutUrls.push(timeoutp+url);
				testUrlFlag = true;
			}
			if(result.flag != true){
				diffUrls.push(url);
				testUrlFlag = true;
			}
			if(testUrlFlag == true){
				jQuery('#'+diffId).append(templateShowObj);
			}
		}
		
		jQuery('#errorUrl').html(errorUrls.join('<br>'));
		jQuery('#timeoutUrl').html(timeoutUrls.join('<br>'));
		jQuery('#diffUrl').html(diffUrls.join('<br>'));
		//jQuery('#btnListPage').click();
	}
	
	function openDetail(obj){
		var status = jQuery(obj).attr('status');
		var tableObj = jQuery('[name="tablechildList3"]',jQuery(obj).parents('[name="templateShowDiv"]')[0]);
		if(status == 0){
			tableObj.show();
			status = 1;
		}else{
			tableObj.hide();
			status = 0;
		}
		jQuery(obj).attr('status',status);
	}
	
	function showList(){
		var tableListObj = jQuery('#contentTable');
		var status = tableListObj.attr('status');
		status = ( status==null || status == 1 )?0:1;
		tableListObj.attr('status',status);
		if(status == 1){
			tableListObj.show();
		}else{
			tableListObj.hide();
		}
	}
	
	function loadShowDiffHtmlObj(templateShowObj,result,url){
		var showDatas = result.showDatas;
		var len = showDatas.length;
		//var templateShowObj = jQuery('#templateShowId').clone();
		var redClass0 = result.flag?"":"redClass";
		jQuery('[name="url"]', templateShowObj).html(url);
		jQuery('[name="url"]', templateShowObj).addClass(redClass0);
		
		var r1Class= (result.r1.status!=200 || result.r1.useTime>2000) ? "errorClass":"";
		var r2Class= (result.r2.status!=200 || result.r2.useTime>2000) ? "errorClass":"";
		var html0 = 
			'<tr name="rowNumPage" class="">'
			+'<td name="num"> 调用信息 </td>'
			+'<td name="p1" class="'+r1Class+'">status: '+result.r1.status+' , useTime: '+result.r1.useTime+'毫秒</td>'
			+'<td name="p2" class="'+r2Class+'">status: '+result.r2.status+' , useTime: '+result.r2.useTime+'毫秒</td>'
			+'</tr>'
			;
		jQuery('[name="tablechildList2"]', templateShowObj).html("");
		jQuery('[name="tablechildList2"]', templateShowObj).append(html0);	
	
		var bodyObj = jQuery('[name="tablechildList3"]', templateShowObj);
		bodyObj.html("");
		for (var i = 0; i < len; i++) {
			var row = showDatas[i];
			var redClass = row.flag?"":"redClass";
			var html = 
				'<tr name="rowNum'+i+'" class="'+redClass+'">'
				+'<td name="num">'+(i+1)+'</td>'
				+'<td name="p1">'+row.p1+'</td>'
				+'<td name="p2">'+row.p2+'</td>'
				+'</tr>'
			;
			bodyObj.append(html);
		}
		jQuery('[name="tablechildList3"]',templateShowObj).hide();
		//templateShowObj.show();
		return templateShowObj;
	}
	
	function getShowDiffHtmlObj(result,url){
		var templateShowObj = jQuery('#templateShowId').clone();
		loadShowDiffHtmlObj(templateShowObj,result,url);
		templateShowObj.show();
		return templateShowObj;
	}
	
	function Compare(r1, r2) {
		var res = {};
		var flags = [];
		var showDatas = [];
		
		var a1 = null;
		var a2 = null;
		try{
			a1 = JSON.stringify(r1.data, null, "\t&nbsp;");
		}catch(e){
			console.info(e);
		}
		try{
			a2 = JSON.stringify(r2.data, null, "\t&nbsp;");
		}catch(e){
			console.info(e);
		}
		
		a1 = a1 == null?[]:a1.split('\t');
		a2 = a2 == null?[]:a2.split('\t');
		a1 = clearBlankData(a1);
		a2 = clearBlankData(a2);
		
		res.a1 = a1;
		res.a2 = a2;
		res.r1 = r1;
		res.r2 = r2;
		res.flags = flags;
		res.showDatas = showDatas;
		
		var len = a1.length>a2.length?a1.length:a2.length;
		var flag0 = true;
		var k1 = 0;
		var k2 = 0;
		for (var i = 0; i < len; i++) {
			var p1 = '';
			var p2 = '';
			if(a1.length>k1){
				p1 = a1[k1];
			}
			if(a2.length>k2){
				p2 = a2[k2];
			}
			
			//console.info(len,i,k1,' = ', p1 , '  ',k2,' = ',p2);
			
			if(p1.replace('&nbsp;','').startsWith('},') && !p2.replace('&nbsp;','').startsWith('},')){
				
				if(!p2.endsWith('}') && a2.length<k2-1){
					p1 = '';
					k1++;
					k2++;
				}else if(p2.endsWith('}')){
					k1++;
				}else{
					p1 = '';
					k2++;
				}
				
			}else if( !p1.replace('&nbsp;','').startsWith('},') && p2.replace('&nbsp;','').startsWith('},') ){
				
				if(!p1.endsWith('}') && a1.length<k1-1){
					p2 = '';
					k2++;
					k1++;
				}else if(p1.endsWith('}')){
					k2++;
					k1++;
				}else{
					p2 = '';
					k1++;
				}
			}else {
				k1++;
				k2++;
			}
			
			//console.info(len,i,k1,k2,' ',p1,' = ',p2);
			
			
		    if((p1 == ''||p1 == '&nbsp;')  && ( p2 == '' ||  p2 == '&nbsp;') ){
				continue;
			}
			
			if(p1 == null && p2 == null){
				continue;
			}
			
			var flag = false;
			if(p1==p2){
				flag = true;
			}
			if((p1 == '&nbsp;"curriculumVersion": 1' || p1== '&nbsp;"curriculumVersion": null'
				|| p1 == '&nbsp;"version": 1' || p1== '&nbsp;"version": null'
				)
			    &&( p2 == '' || p2 == '&nbsp;},' || p2 == '&nbsp;}' )){
				//k2--;
				if(p2 =='&nbsp;}'){
					k2--;
					p2 =='';
				}
				flag = true;
			}
			if(p1 == p2 + ','){
				flag = true;
			}
			flags.push(flag);
			var showData = {p1:p1,p2:p2,flag:flag};
			showDatas.push(showData);
			if(flag == false){
				flag0 = false;
			}
		}
		
		res.flag = flag0;
		return res;
	}
	
	function clearBlankData(a){
		var b = [];
		var len = a.length;
		for (var i = 0; i < len; i++) {
			var e = a[i];
			if(e==null){
				continue;
			}
			if(e=='' || jQuery.trim(e) == ''){
				continue;
			}
			if(e=='&nbsp;'){
				continue;
			}
			e = e.trim();
			b.push(e);
		}
		return b;
	}
	
	function getData(domain,api){
		var requestUrl = domain + api;
		requestUrl = encodeURI(requestUrl);
		console.info(requestUrl);
		var res = {};
		var data = {url:requestUrl,method:"GET"};
		var url = listUrl +"getTestData";
	    jQuery.ajax({
			url: url, async : false,type: "GET",
			data : data,
			success: function(response) {
				var info = response;
				if(info!=null && info.data!=null && info.data!=''){
					try{
						res.data =  eval('('+info.data+')' );
					}catch(e){
						console.error('data to json  ',info.data,' , error = ',e);
					}
				}
				res.status = info.status;
				res.useTime = info.useTime;
				res.message = info.message;
				res.exception = info.exception;
			},
			error: function(e){console.error(e);}
		});
	    return res;
	}
	
	
	
	

	
</script>
<script src="${ctxStatic}/common/wsize.min.js" type="text/javascript"></script>


</body>
</html>